export const uuid = (len: number, radix?: number, onlyLowerCase?: boolean) => {
    let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    if (onlyLowerCase) {
        chars = 'abcdefghijklmnopqrstuvwxyz'.split('');
    }
    const uuid = [];
    let i;
    radix = radix || chars.length;
    if (len) {
// Compact form
        for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
    } else {
// rfc4122, version 4 form
        let r;
// rfc4122 requires these characters
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';
// Fill in random data.  At i==19 set the high bits of clock sequence as
// per rfc4122, sec. 4.1.5
        for (i = 0; i < 36; i++) {
            if (!uuid[i]) {
                r = 0 | Math.random() * 16;
                uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
            }
        }
    }
    return uuid.join('');
}
export const zero = (n: number) => {
    return n < 10 ? `0${n}` : n
}
export const formatFileDate = (date: Date, char: string) => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    return `${year}${char}${zero(month)}${char}${zero(day)}${char}${zero(hour)}${char}${zero(minute)}${char}${zero(second)}`
}
export const formatTs = (ts: number) => {
    const date = new Date(ts)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    return `${year}-${zero(month)}-${zero(day)} ${zero(hour)}:${zero(minute)}:${zero(second)}`
}
export const loopTree = (treeData: any[], childrenKey: string = 'children', fun?: Function) => {
    const loop = (nodeList: any[], level: number, parentNode?: any) => {
        const size = nodeList.length
        for(let i=0; i<size; i++) {
            fun && fun(nodeList[i], {level, parent:parentNode})
            if (nodeList[i][childrenKey] && nodeList[i][childrenKey].length) {
                loop(nodeList[i][childrenKey], level+1, nodeList[i])
            }
        }
    }
    loop(treeData, 1)
}
export const filterTree = (str: string, labelKey: string, treeData: any[], fn?: Function) => {
    if (!treeData) {
        return []
    }
    let newList:any[] = [];
    treeData.forEach((item:any) => {
        if (item[labelKey].indexOf(str) > -1) {
            const children = filterTree(str, labelKey, item.children);
            const obj = {
                ...item,
                children: children
            }
            newList.push(obj);
            fn && fn(obj)
        } else {
            if (item.children && item.children.length > 0) {
                const children = filterTree(str, labelKey, item.children);
                const obj = {
                    ...item,
                    children: children
                };
                if (children && children.length > 0) {
                    newList.push(obj);
                    fn && fn(obj)
                }
            }
        }
    });
    return newList;
}
export const isNum = (n: any) => {
    return typeof n === 'number' && isFinite(n)
}
export function judgeCanUse(start: string, end: string, usedTime: string[]) {
    const today = new Date()
    const todayStr = today.getFullYear() + `-${today.getMonth()+1}` + `-${today.getDate()}`
    const intersection: string[] = [] // 缓存冲突的时间段
    usedTime.forEach((tm) => {
        const ta = tm.split('-')
        const ts = new Date(`${todayStr} ${ta[0]}:00`).getTime() // 时间段起点
        const te = new Date(`${todayStr} ${ta[1]}:00`).getTime() // 时间段终点
        const st = new Date(`${todayStr} ${start}:00`).getTime() // 开始时间
        const et = new Date(`${todayStr} ${end}:00`).getTime() // 结束时间
        /* 开始时间在时间段终点后 或 结束时间在时间段起点前 */
        if (st >= te || et <= ts) {

        } else {
            intersection.push(tm)
        }
    })
    // console.log(start, end, intersection)
    return intersection
}
